﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace YATC
{
    public class Camera
    {
        public Vector3 position;
        float myYaw;
        float myPitch;
        public float _x, _y, _z;

        public float Yaw
        {
            get
            {
                return myYaw;
            }
            set
            {
                myYaw = value;
                if (myYaw >= MathHelper.Pi * 2)
                    myYaw -= MathHelper.Pi * 2;
                else if (myYaw < 0)
                    myYaw += MathHelper.Pi * 2;
            }
        }
        public float Pitch
        {
            get
            {
                return myPitch;
            }
            set
            {
                myPitch = value;
                if (myPitch >= MathHelper.PiOver2)
                    myPitch = MathHelper.PiOver2;
                else if (myPitch < -MathHelper.PiOver2)
                    myPitch = -MathHelper.PiOver2;
            }
        }
        public float speed;
        Matrix myViewMatrix;
        public Matrix ViewMatrix
        {
            get
            {
                return myViewMatrix;
            }
        }
        public Matrix projectionMatrix;

        Matrix myWorldMatrix;

        public Matrix WorldMatrix
        {
            get
            {
                return myWorldMatrix;
            }
        }

        public Camera(Vector3 position, float yaw, float pitch, float speed)
        {
            _x = position.X;
            _z = position.Z;
            _y = position.Y;
            this.position = position;
            myYaw = yaw;
            myPitch = pitch;
            this.speed = speed;
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 4f / 3f, .1f, 10000.0f);
            Mouse.SetPosition(200, 200);
        }
        public void Initialise()
        {
            position = new Vector3(0f, -1.98f, 30.72f); 
            myYaw = 0.01f;
            myPitch = 0.095f;
            speed = 10;
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 4f / 3f, .1f, 10000.0f);
            Mouse.SetPosition(200, 200);
            _x = position.X;
            _z = position.Z;
            _y = position.Y;

        }
        public void MoveForward(float dt)
        {
            position += myWorldMatrix.Forward * (dt * speed);
        }

        public void MoveRight(float dt)
        {
            position += myWorldMatrix.Right * (dt * speed);
        }

        public void MoveUp(float dt)
        {
            position += new Vector3(0, (dt * speed), 0);
        }

        public void Update(float dt)
        {
            myViewMatrix = Matrix.CreateTranslation(-position) * Matrix.CreateFromAxisAngle(Vector3.Up, -Yaw) * Matrix.CreateFromAxisAngle(Vector3.Right, -Pitch);
            myWorldMatrix = Matrix.Invert(myViewMatrix);
        }

        private int vn = 1;



        public Camera(Vector3 view)
        {
            _x = view.X;
            _y = view.Y;
            _z = view.Z;
        }

        public void ResetCamera()
        {
            _x = 0;
            _y = 0;
            _z = 20;
            
        }
        public void NextView()
        {
            switch (vn)
            {
                case 0:
                    Center();
                    break;
                case 1:
                    Right();
                    break;
                case 2:
                    Left();
                    break;
                case 3:
                    Up();
                    break;
                case 4:
                    Down();
                    break;
                    
            }
            vn++;
            if (vn == 5)
                vn = 0;
        }
        public void Update()
        {
            myViewMatrix = Matrix.CreateLookAt(new Vector3(_x,_y,_z), Vector3.Zero, Vector3.Up);
        }
        private void Center()
        {
            _x = 0;
            _y = 0;
            _z = 20;
        }
        private void Right()
        {
            _x = 15;
            _y = 0;
            _z = 5;   
        }
        private void Left()
        {
            _x = -20;
            _y = 5;
            _z = 15;
        }
        private void Up()
        {
            _x = 0;
            _y = 10;
            _z = 20;
        }
        private void Down()
        {
            _x = 0;
            _y = -10;
            _z = 20;
        }
    }
}
